package Optional;

import Lambda.Employee;
import org.junit.Test;

import java.util.Optional;
import java.util.function.Consumer;

public class OptionalDemo {

    @Test
    public void test1(){
        Optional<Employee> employee = Optional.of(new Employee()); //创建一个Optional实例
        System.out.println(employee.get());

        Optional<Object> empty = Optional.empty();  //创建一个空的Optional实例。
        // System.out.println(empty.get()); //这里会发生空指针异常 ，因为empty是null

        Optional<Object> o = Optional.ofNullable(null); //传进去的这个为null，那就是空实例；否则就不是。
         // System.out.println(o.get());  //出现 java.util.NoSuchElementException: No value present异常。
        Object o1 = o.orElseGet(Employee::new);
        System.out.println(o1);

        Optional<Employee> employee1 = Optional.ofNullable(new Employee(1, "小李", 25, 40000, "技术部", Employee.Status.BUSY));
        System.out.println(employee1.get());

        employee1.map(Employee::getName).ifPresent(System.out::println);
    }

    public String getEmployeeName(Employee employee){
        return employee==null?null:employee.getName();
    }

    @Test
    public void test2(){
        System.out.println(getEmployeeName(null));
    }

    public String getEmployeeName2(Optional<Employee> employee){
        return employee.orElseGet(Employee::new).getName();
    }

    @Test
    public void test3(){
        Employee employee=null;
        System.out.println(getEmployeeName2(Optional.ofNullable(employee)));
    }

    @Test
    public void test4() {
        Optional<Employee> employeeOptional = Optional.of(new Employee());
        employeeOptional.ifPresent(System.out::println); // 如果employeeOptional.get()返回不为null，则输出它的值。反之，如果为null，则不执行输出语句.
        // 这里ifPresent()的入参是一个Consumer接口
        Employee b = new Employee();
        Consumer<String> a = b::setName;
        Consumer<String> stringConsumer = a.andThen(System.out::println);
        stringConsumer.accept(employeeOptional.map(Employee::getName).orElse("No Name"));
        System.out.println(b);
    }

    @Test
    public void testOptionalMap() {
        Employee employee = null;
        Employee employee1 = new Employee();
        employee1.setName("马弓手关某");
        Optional.ofNullable(employee).map(Employee::getName).ifPresent(System.out::println);
        String employeeName = Optional.ofNullable(employee).map(Employee::getName).orElse("No Name");
        System.out.println(employeeName);
        employeeName = Optional.ofNullable(employee).map(Employee::getName).orElseGet(() -> Optional.ofNullable(employee1).map(Employee::getName).orElse("No Name"));
        System.out.println(employeeName);
        try {
            employeeName = Optional.ofNullable(employee).map(Employee::getName).orElseThrow(() -> new RuntimeException("This employee no mame"));
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }

    @Test
    public void testEmptyOptional() {
        //初始化时应该使用Optional.empty()创建
        Optional<Employee> employee = Optional.empty();
        employee.ifPresent(System.out::println);
    }

}

